package com.android.takeaway.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.List;

import com.android.takeaway.R;
import com.android.takeaway.activity.ShopDetailActivity;
import com.android.takeaway.bean.Shop;

/**
 * 店铺适配器
 */
public class ShopAdapter extends RecyclerView.Adapter<ShopAdapter.MyViewHolder> {
    private List<Shop> shopList;  //商店列表数据
    private Context context; //上下文参数

    //构造方法
    public ShopAdapter(Context context) {   //给上下文对象赋值
        this.context = context;
    }

    //给商店列表赋值
    public void setData(List<Shop> shopList) {
        this.shopList = shopList;
        notifyDataSetChanged(); //让界面重新刷新
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.shop_item, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    //把值显示在界面上
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        Shop shopBean = shopList.get(position);
        // 获取图片的资源引用
        int resourceId = context.getResources().getIdentifier(shopBean.getShopPic(), "mipmap", context.getPackageName());
        //加载图像到iv_shopPic控件
        Glide.with(context).load(resourceId).into(holder.iv_shopPic);
        holder.tv_shopName.setText(shopBean.getShopName());
        holder.tv_saleNum.setText("月销量： " + shopBean.getSaleNum());
        holder.tv_officePrice.setText("起送价格： " + shopBean.getOfferPrice());
        holder.tv_distributionCost.setText("配送费：" + shopBean.getDistributionCost());
        holder.tv_welfare.setText(shopBean.getWelfare());
        holder.tv_time.setText(shopBean.getTime());

        holder.itemView.setOnClickListener(new View.OnClickListener() { //监听器
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ShopDetailActivity.class);
                intent.putExtra("shopBean", shopBean);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        //判断list里面是否为空，如果是空则返回0否则返回本身长度
        return shopList == null ? 0 : shopList.size();
    }


    public class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_shopPic;
        TextView tv_shopName, tv_saleNum, tv_officePrice, tv_distributionCost, tv_welfare, tv_time;

        //构造方法，获取视图组件的引用
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_shopPic = itemView.findViewById(R.id.iv_shopPic);
            tv_shopName = itemView.findViewById(R.id.tv_shopName);
            tv_saleNum = itemView.findViewById(R.id.tv_saleNum);
            tv_officePrice = itemView.findViewById(R.id.tv_offerPrice);
            tv_distributionCost = itemView.findViewById(R.id.tv_distributionCost);
            tv_welfare = itemView.findViewById(R.id.tv_welfare);
            tv_time = itemView.findViewById(R.id.tv_time);
        }
    }
}
